<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\CategoryRequest;
use App\Models\Category;

class CategoryController extends Controller
{
    public function index()
    {
    	$datas = Category::top()->with('subcates')->get();
    	return view('dashboard.category.index')->with('datas', $datas);
    }

    public function create()
	{
		return view('dashboard.category.create');
	}
	
    public function store(CategoryRequest $request)
    {
    	Category::create($request->all());
    	return redirect()->route('admin.categories.index');
    }

    public function edit(Category $category)
    {
    	return view('dashboard.category.create', ['data' => $category]);
    }

    public function update(CategoryRequest $request, Category $category)
    {
    	$category->update($request->all());
    	return redirect()->route('admin.categories.index');
    }

    public function destroy(Category $category) {
        // if($category->goods()->exist())
        // {
        //     return response()->json(['status' => 1]);
        // }
        $category->delete();
        return response()->json(['status' => 0]);
    }
}
